<?php

/*************************************************************************

	FILE INCLUDE

**************************************************************************/
include_once '../lib/Template.class.php';
$debug_mode_value = 0;
checkLogin();

sajax_init();
$sajax_debug_mode = 0;
sajax_export("addCandidate");
sajax_export("delCandidate");
sajax_export("changeRepresent");
sajax_export("getCandidate");
sajax_handle_client_request();

/*************************************************************************

	USER SCRIPT

***************************************************************************/
$userscript =<<<js
    <script type="text/javascript">
        <!--
			function addC(){
				candi = document.getElementById("addCandidate").value;
				cluster_cd = document.getElementById("cluster_cd").value;
				x_addCandidate( candi,cluster_cd, closeWindow );
			}
			function closeWindow(result){
				if(result){
					var cluster_cd = result;
					window.opener.getCandidate(cluster_cd);
					window.open('about:blank','_self').self.close();
				}
			}

			function delCandidate(cluster_cd){
				if(confirm("Are you sure you want to delete it?")){
					name="chk_"+cluster_cd;
					obj=document.getElementsByName(name);
					for(i=0;i<obj.length;i++){
						if(obj[i].checked){
							cadidate_cd = obj[i].value;
							x_delCandidate( cadidate_cd,cluster_cd, reloadCandidate );
						}
					}
				}
			}
			function reloadCandidate(result){
				getCandidate(result);
			}
            function changeRepresent(cluster_cd){
            	name="chk_"+cluster_cd;
				obj=document.getElementsByName(name);
				for(i=0;i<obj.length;i++){
					if(obj[i].checked){
						cadidate_cd = obj[i].value;
						x_changeRepresent( cadidate_cd,cluster_cd, reloadCandidate );
					}
				}
            }
			function getCandidate(cluster_cd){
				x_getCandidate( cluster_cd, setCandidate );
			}

			function setCandidate(result){
				var htm="";
				var represent=""
				for(var i in result){
					cluster_cd = result[i].CLUSTER_CD;
					if(result[i].CANDIDATE_NM != ""){
						htm += "<input name='chk_"+result[i].CLUSTER_CD+"' type='checkbox' value='"+result[i].CANDIDATE_CD+"'> "+result[i].CANDIDATE_NM+"&nbsp;&nbsp;&nbsp;&nbsp;"
						if(result[i].REPRESENT_FL == 1){
							represent = result[i].CANDIDATE_NM;
						}
					}
				}
				name1 = "cadidate_td_"+cluster_cd;
				name2 = "represent_td_"+cluster_cd;
				document.getElementById(name1).innerHTML=htm;
				document.getElementById(name2).innerHTML=represent;
			}

        //-->
    </script>
js;


/*************************************************************************

		PAGE MAIN PROCESS

***************************************************************************/
(!empty($_REQUEST['cluster_cd']))? $cluster_cd = $_REQUEST['cluster_cd'] : $cluster_cd = "";
(!empty($_REQUEST['search_candidate']))? $search_candidate = htmlspecialchars($_REQUEST['search_candidate']) : $search_candidate = "";

$search_flag=false;

if(!empty($search_candidate)){
	$db->conn();
	$sql = "select a.*,b.CANDIDATE_CD, b.CANDIDATE_NM, d.NER_NM
			from cluster_tb a
				left outer join candidate_tb b
				on a.CLUSTER_CD = b.CLUSTER_CD
				and b.REPRESENT_FL = 1
 					left outer join ner_tb d
				    on a.NER_CD = d.NER_CD
			where a.CLUSTER_CD in (SELECT CLUSTER_CD FROM candidate_tb WHERE CANDIDATE_NM='$search_candidate')
			order by cluster_cd desc";
	$rs = $db->query($sql);
	$num = $db->num($rs);
	$list = array();
	if( $num > 0 )
	{
		for( $i = 0; $i < $num; $i++)
		{
			$list[$i] = $db->fetch_assoc($rs);
			$cluster=$list[$i][CLUSTER_CD];
			$list[$i][candidate] = $db->select("select CANDIDATE_CD, CANDIDATE_NM from candidate_tb where cluster_cd=$cluster");
		}
	}
	else
	{
		$list = "";
		$search_flag=true;
	}
	$db->close();
}



/*************************************************************************

	TEMPLATE PROCESS

***************************************************************************/
$body = "PopupCandidate.html";

$tpl = new Template();
$tpl->setDefine2($body);

$tpl->assign("cluster_cd",$cluster_cd);
$tpl->assign("search_flag",$search_flag);
$tpl->assign("list", $list);
$tpl->assign("search_candidate", $search_candidate);

$tpl->setPrint2();


/*************************************************************************

	AJAX FUNCTION : Template_ function

***************************************************************************/

function addCandidate($candi,$cluster_cd){
	global $db;

	$candi = Unescape($candi);

	$db->conn();

	$db->query("INSERT INTO candidate_tb
			(CLUSTER_CD, CANDIDATE_NM, CANDIDATE_ST, REPRESENT_FL)
			VALUES ($cluster_cd, '$candi', 1, 0)");
	$candidate_id = mysql_insert_id();
	$db->query("INSERT INTO candidate_history_tb
			(CANDIDATE_CD, OPERATION, USER_CD, REGISTER_DT)
			VALUES ($candidate_id, 1, $_SESSION[user_cd], now())");
	$db->close();

	return $cluster_cd;
}

function delCandidate($candidate_cd,$cluster_cd){
	global $db;

	$db->conn();
	$rt=$db->fetch("select candidate_nm from candidate_tb where CANDIDATE_CD = $candidate_cd");
	$db->query("INSERT INTO candidate_history_tb
				(CANDIDATE_CD, OPERATION, USER_CD, REGISTER_DT,ETC)
				VALUES ($candidate_cd, 9, $_SESSION[user_cd], now(),'$rt[candidate_nm]')");
	$db->query("DELETE FROM candidate_tb WHERE CANDIDATE_CD = $candidate_cd");
	$db->close();

	return $cluster_cd;
}

function changeRepresent($candidate_cd,$cluster_cd){
	global $db;

	$db->conn();
	$db->query("UPDATE candidate_tb SET REPRESENT_FL = 0	WHERE CLUSTER_CD = $cluster_cd");
	$db->query("UPDATE candidate_tb SET REPRESENT_FL = 1	WHERE CANDIDATE_CD = $candidate_cd");
	$db->close();

	return $cluster_cd;
}

function setNer($ner_cd,$cluster_cd){
	global $db;

	$db->conn();
	$db->query("UPDATE cluster_tb SET NER_CD = $ner_cd	WHERE CLUSTER_CD = $cluster_cd");
	$db->close();

}
function getCandidate($cluster_cd){
	global $db;

	$db->conn();
	$list = $db->select("select CLUSTER_CD, CANDIDATE_CD, CANDIDATE_NM, REPRESENT_FL from candidate_tb where cluster_cd=$cluster_cd");
	if(empty($list)){
			$list[0][CLUSTER_CD]=$cluster_cd;
			$list[0][CANDIDATE_NM]="";
	}
	$db->close();
	return $list;
}

?>
